<?php
class options_helper {

	/**
	 * 从数组中根据$field键名，取出在$option_types中的所有数组
	 **/
	static function returnOptionsFromArray( $arr, $field, $option_types ) {
		$rows	= array();
		foreach ($arr as $key => $row) {
			if (isset($row[$field]) && !empty($row[$field])) {
				foreach ($option_types as $option_type) {
					if ($row[$field] == $option_type) {
						$rows[$option_type][]	= $row;
					}
				}
			}
		}
		return $rows;
	}

	static function fieldDatas($data,$field,$usekey=false) {
		$retData=array();
		foreach ($data as $key=> $row) {
			if(is_array($field)){
				$retData[ $row[key($field)] ]=$row[current($field)];
			}
			else if($usekey){
				if( $usekey=='row' ){
					$retData[ $row[$field] ]=$row;
				}
				else{
					$retData[$key]=$row[$field];
				}
			}
			else{
				$retData[]=$row[$field];
			}
		}
		return $retData;
	}


	static function returnOptionsByCode($arr, $onlyName=false)
	{
	    $rows = array();
	    foreach($arr as $row) {
	        if ($onlyName) {
	        	$rows[$row['option_id']] = $row['option_name'];
	        } else {
	            $rows[$row['option_id']] = $row;
	        }

	    }
	    return $rows;
	}
}